<%@page import="org.ielts.domain.base.Teaching"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.ielts.domain.security.User"%>
<%@page import="org.ielts.domain.base.Classroom"%>
<%@page import="org.ielts.domain.base.CourseType"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.springframework.validation.BindException"%>
<%@page import="org.ielts.domain.base.Lesson"%>
<%@page import="java.util.List"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%><%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<tiles:insertDefinition name="layout">
	<tiles:putAttribute name="head">
		<script type="text/javascript">
			$(function() {
				/*
				$(".lessonTime").timepicker({
					defaultTime : '00:00',
					showMeridian : false
				});
				$(".date").datepicker({
					showWeek : true,
					firstDay : 1,
					dateFormat : 'yy-mm-dd',
					showOtherMonths : true,
					selectOtherMonths : true,
				});
				 */

				$(".remove").click(function() {
					$(this).closest("tr").remove();
				});

				var dialog;

				function startPost(queue, index) {
					var lesson = queue[index];
					dialog.content("正在上传" + (index + 1) + '/' + queue.length + "条记录...");
					$.post("doImportExcel", lesson, function(data) {
						if (data.success) {
							if (index < (queue.length - 1)) {
								startPost(queue, ++index);
							} else {
								dialog.close();
								helper.success({
									title : '上传结束',
									content : "导入所有课表成功!"
								});
							}
						}
					});
				}

				$("#save").click(function() {
					var queue = [];

					$("#selectable tr.lesson").each(function() {
						var lesson = {};

						lesson.day = $(this).find("[name=day]").val();
						lesson.beginTime = $(this).find("[name=beginTime]").val();
						lesson.endTime = $(this).find("[name=endTime]").val();
						lesson.name = $(this).find("[name=name]").val();
						lesson.no = $(this).find("[name=no]").val();

						var teachers = $(this).find("[name=teacher]");
						var teacherIds = [];
						teachers.each(function() {
							teacherIds.push($(this).val());
						});

						lesson.teachers = teacherIds.join(",");

						lesson.classroom = $(this).find("[name=classroom]").val();
						lesson.courseType = $(this).find("[name=courseType]").val();

						queue.push(lesson);
					});

					dialog = art.dialog({
						content : '开始上传...'
					});

					startPost(queue, 0);
				});
			});
		</script>
	</tiles:putAttribute>
	<tiles:putAttribute name="mainBody">
		<ul class="breadcrumb">
			<li><a href="listLesson">课表管理</a> <span class="divider">/</span></li>
			<li class="active">导入课表</li>
		</ul>
		<form method="post" action="importExcel" enctype="multipart/form-data" class="form-horizontal">
			<div class="control-group">
				<label class="control-label">文件上传</label>
				<div class="controls">
					<input name="excel" type="file" title="选择Excel文件" />
					<button type="submit" class="btn btn-primary">提交文件</button>
				</div>
			</div>
		</form>
		<div class="alert alert-block">
			点击”选择一个Excel文件“，提交一个形如
			<a href="lessons.xls">lessons.xls</a>
			的Excel文件。然后再点击”提交文件“按钮，预览导入课表的情况。
		</div>
		<s:if test="#lessons.size > 0">
			<button id="save" class="btn btn-primary">保存课表</button>
			<span class="help-block">点击该按钮确认导入课表</span>
		</s:if>
		<table class="table">
			<thead>
				<tr>
					<th>No</th>
					<th>Day</th>
					<th>Time</th>
					<th>Lesson</th>
					<th>Teacher</th>
					<th>Classroom</th>
					<th>Type</th>
				</tr>
			</thead>
			<tbody id="selectable">
				<%
					ActionContext actionContext = ActionContext.getContext();
							List<Lesson> lessons = (List<Lesson>) actionContext.get("lessons");
							List<BindException> bindExceptions = (List<BindException>) actionContext.get("bindExceptions");
							SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

							if (lessons != null) {
								for (int i = 0; i < lessons.size(); i++) {
									Lesson lesson = lessons.get(i);
									BindException bindException = bindExceptions.get(i);
									String day = simpleDateFormat.format(lesson.getBeginTime());
				%>
				<tr class="lesson">
					<td><a href="#" class="remove">
							<i class="icon icon-remove"></i>
						</a><input type="text" value="<%=i + 1%>" name="no" class="input-mini" /></td>
					<td><input type="text" name="day" value="<%=day%>" class="date input-small" /></td>
					<td>
						<div class="input-append bootstrap-timepicker">
							<input name="beginTime" class="lessonTime input-mini" value="<%=lesson.getBeginTimeHHMM()%>" type="text"> <span class="add-on"> <i class="icon-time"></i>
							</span>
						</div>-
						<div class="input-append bootstrap-timepicker">
							<input name="endTime" class="lessonTime input-mini" value="<%=lesson.getEndTimeHHMM()%>" type="text"> <span class="add-on"> <i class="icon-time"></i>
							</span>
						</div> <%
 	if (bindException.getFieldError("beginTime") != null) {
 %>
						<div class="alert alert-error">
							<%=bindException.getFieldError("beginTime").getDefaultMessage()%>
						</div> <%
 	}
 %>
					</td>
					<td><input type="text" value="<%=lesson.getName()%>" name="name" /> <%
 	if (bindException.getFieldError("name") != null) {
 %><div class="alert alert-error">
							<%=bindException.getFieldError("name").getDefaultMessage()%>
						</div> <%
 	}
 %></td>
					<td>
						<%
							if (lesson.getTeachings().size() == 0) {
						%> <select class="input-small" name="teacher">
							<%
								Iterable<User> teachers = (Iterable<User>) actionContext.get("teachers");
													for (User t : teachers) {
							%><option value="<%=t.getId()%>"><%=t.getName()%>
								<%
									if (StringUtils.isNotBlank(t.getNo())) {
								%>
								(<%=t.getNo()%>)
								<%
									}
								%>
							</option>
							<%
								}
							%>
					</select>
						<div class="alert alert-error">没有导入授课教员</div> <%
 	} else {
 						for (Teaching teaching : lesson.getTeachings()) {
 %>
						<div>
							<select class="input-small" name="teacher">
								<%
									Iterable<User> teachers = (Iterable<User>) actionContext.get("teachers");
															for (User t : teachers) {
								%>
								<option value="<%=t.getId()%>" <%if (teaching.getTeacher().equals(t)) {%> selected="selected" <%}%>>
									<%=t.getName()%>
									<%
										if (StringUtils.isNotBlank(t.getNo())) {
									%>
									(<%=t.getNo()%>)
									<%
										}
									%>
								</option>
								<%
									}
								%>
							</select>
						</div> <%
 	}
 %> <%
 	if (bindException.getFieldError("teachings") != null) {
 %>
						<div class="alert alert-error">
							<%=bindException.getFieldError("teachings").getDefaultMessage()%>
						</div> <%
 	}
 					}
 %>
					</td>
					<td><select class="input-medium" name="classroom">
							<%
								Iterable<Classroom> classrooms = (Iterable<Classroom>) actionContext.get("classrooms");
												for (Classroom classroom : classrooms) {
							%>
							<option value="<%=classroom.getId()%>" <%if (lesson.getClassroom().equals(classroom)) {%> selected="selected" <%}%>><%=classroom.getName()%></option>
							<%
								}
							%>
					</select> <%
 	if (bindException.getFieldError("classroom") != null) {
 %>
						<div class="alert alert-error">
							<%=bindException.getFieldError("classroom").getDefaultMessage()%>
						</div> <%
 	}
 %></td>
					<td><select class="input-small" name="courseType">
							<%
								Iterable<CourseType> courseTypes = (Iterable<CourseType>) actionContext.get("courseTypes");
												for (CourseType courseType : courseTypes) {
							%>
							<option value="<%=courseType.getId()%>"><%=courseType.getName()%></option>
							<%
								}
							%>
					</select></td>
				</tr>
				<%
					}
							}
				%>
			</tbody>
		</table>
	</tiles:putAttribute>
</tiles:insertDefinition>